<?php

namespace app\selladmin\controller;

use app\selladmin\controller\CommonController;
use app\selladmin\model\Custom;
use app\selladmin\model\Store;
use app\selladmin\model\Item;
use app\selladmin\model\OrdPro;
use app\selladmin\model\Order;
use app\selladmin\model\CtmStaff;

class CustomController extends CommonController {
    /*
     * 客户列表 
     * author xfwed
     * date 2017.08.09
     */

    public function customlist() {
        //查询员工信息
        $custom = new Custom();
        $custom_name = input('get.custom_name');
        $custom_phone = input('get.custom_phone');
        $customList = $custom->CustomInfo('', '', $custom_name, $custom_phone, 13);
        $aa = ($customList->toArray()['data']);
        for ($i = 0; $i < count($aa); $i++) {
            //查询店铺信息
            $store = new Store();
            $aa[$i]['storeinfo'] = $store->StoreInfo($aa[$i]['store_id']);
        }
        return view('customlist', [
            'aa' => $aa,
            'customList' => $customList,
            'custom_name' => $custom_name,
            'custom_phone' => $custom_phone,
        ]);
    }

    /*
     * 客户信息
     * author xfwed
     * date 2017.08.09
     */

    public function ShowMessage() {
        $id = input('param.id'); //用户id
        //查询用户信息
        $custom = new Custom();
        $custominfo = $custom->CustomInfo($id, '', '', '', '');
//        echo "<pre />";
//        print_r($custominfo->toArray()['data'][0]['custom_photo']);
        //查询订单信息
        $order = new Order();
        $ord_num = input('get.ord_num');
        $orderList = $order->OrderInfo('', $ord_num, $id, 10);

        return view('showmessage', [
            'custominfo' => $custominfo->toArray()['data'][0],
            'orderList' => $orderList,
            'ord_num' => $ord_num,
            'id' => $id,
        ]);
    }

    /*
     * 查看预约信息
     * author xfwed 
     * date 2017.08.09
     */

    public function ShowOnlineMessage() {
        $id = input('param.id');
        //查询用户信息
        $custom = new Custom();
        $custominfo = $custom->CustomInfo($id, '', '', '', '');
        //查询员工信息
        $ctmstaff = new CtmStaff();
        $onlineList = $ctmstaff->OnlineInfo($id, '', '', 13);
        return view('showonlinemessage', [
            'custominfo' => $custominfo->toArray()['data'][0],
            'onlineList' => $onlineList,
            'id' => $id,
        ]);
    }

//    /*
//     * 用户分析
//     * author xfwed
//     * date 2018-01-10
//     */
//    public function CustomEcharts(){
//        return view('');
//    }
//    
    /*
     * 图表展示
     * author xfwed
     * date 2018-01-12
     * */

    public function CustomEcharts() {
        $year = isset($_REQUEST['year']) ? $_REQUEST['year'] : date('Y');
        //月份
        for ($m = 1; $m <= 12; $m++) {
            $month[] = $m . "月";
        }

        //用户量（总）
        $customData = Custom::where("FROM_UNIXTIME(add_time,'%Y') = '" . $year . "'")
                ->where('status','=',2)
                ->field("FROM_UNIXTIME(add_time, '%m') AS m,count(id) AS c")
                ->group('m')
                ->order('m asc')
                ->select();
        $cusReturn_data = array(
            '01' => 0, '02' => 0, '03' => 0, '04' => 0, '05' => 0, '06' => 0, '07' => 0, '08' => 0, '09' => 0, '10' => 0, '11' => 0, '12' => 0,
        );
        foreach ($customData as $value) {
            $cusReturn_data[$value['m']] = $value['c'];
        }

        //用户量（男）
        $customData1 = Custom::where("FROM_UNIXTIME(add_time,'%Y') = '" . $year . "'")
                ->where('custom_sex', '=', 1)
                ->where('status','=',2)
                ->field("FROM_UNIXTIME(add_time, '%m') AS m,count(id) AS c")
                ->group('m')
                ->order('m asc')
                ->select();
        $cusReturn_data1 = array(
            '01' => 0, '02' => 0, '03' => 0, '04' => 0, '05' => 0, '06' => 0, '07' => 0, '08' => 0, '09' => 0, '10' => 0, '11' => 0, '12' => 0,
        );
        foreach ($customData1 as $value) {
            $cusReturn_data1[$value['m']] = $value['c'];
        }

        //用户量（女）
        $customData2 = Custom::where("FROM_UNIXTIME(add_time,'%Y') = '" . $year . "'")
                ->where('custom_sex', '=', 2)
                ->where('status','=',2)
                ->field("FROM_UNIXTIME(add_time, '%m') AS m,count(id) AS c")
                ->group('m')
                ->order('m asc')
                ->select();
        $cusReturn_data2 = array(
            '01' => 0, '02' => 0, '03' => 0, '04' => 0, '05' => 0, '06' => 0, '07' => 0, '08' => 0, '09' => 0, '10' => 0, '11' => 0, '12' => 0,
        );
        foreach ($customData2 as $value) {
            $cusReturn_data2[$value['m']] = $value['c'];
        }


        $currentYear = date('Y');
        $years = $currentYear + 1;

        return view('customecharts', [
            'month' => implode(',', $month),
            'cusData' => implode(',', array_values($cusReturn_data)),//用户量总
            'cusData1' => implode(',', array_values($cusReturn_data1)),//用户量男
            'cusData2' => implode(',', array_values($cusReturn_data2)),//用户量女
            'years' => $years,
            'year' => $year,
            'currentYear' => $currentYear
        ]);
    }

}
